package thread;
/**
 * 双重锁检查保证多线程环境下单例模式的准确性
 */

import java.util.concurrent.ThreadLocalRandom;
public class DCLTest {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                DclService dclService= DclService.getInstance();
                System.out.println(dclService.id);
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                DclService dclService= DclService.getInstance();
                System.out.println(dclService.id);
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                DclService dclService= DclService.getInstance();
                System.out.println(dclService.id);
            }
        }).start();
    }
}


class DclService{

    volatile private static DclService dclService;

    public  int id;

    private DclService(){
        id= ThreadLocalRandom.current().nextInt(100000);
    }

    public static DclService getInstance(){
        if(dclService==null){
            synchronized (DclService.class){
                if(dclService==null){
                    dclService=new DclService();
                }
            }
        }
        return dclService;
    }
}